#include "io.h"

void _io_init(void);
void _io_exit(void);

//
// Highscores
//


// 10 scores par default

void _io_read_highscores();
void _io_write_highscores();

void _io_read_config(void);
void _io_write_config(void);

Highscore _io_scores_default[10] = // score des bots, ceux par default
{
    {"ben", 50},
    {"jul", 50},
    {"rem", 50},
    {"kev", 50},
    {"j-b", 50},
    {"aaa", 50},
    {"aaa", 50},
    {"aaa", 50},
    {"aaa", 50},
    {"aaa", 50},
};

Highscore _io_scores[10]; // score des joueurs


void io_highscore_read(Highscore *score, int id)
{
    memcpy(score, &_io_scores[id], sizeof(Highscore));
}

void io_highscore_write(Highscore *score)
{
    int i = 0;
    if(io_highscore_eligible(score->score) == 0)
    {
        return;
    }
    //boucle tant que le score est pas strictement supÈrieur ‡ celui d'en dessous| Puis
    while (score->score <_io_scores[i].score)
    {
        i++;
    }
    memmove(&_io_scores[i+1], &_io_scores[i], sizeof(Highscore)*(10-i-1));
    memcpy(&_io_scores[i], score, sizeof(Highscore));
}

void io_highscore_reset(void)
{
    memcpy(_io_scores, _io_scores_default, sizeof(_io_scores));
}

int io_highscore_eligible(int score)
{
    int i = 0;
    //boucle tant que le score est pas strictement supÈrieur ‡ celui d'en dessous| Puis
    while (score <_io_scores[i].score)
    {
        i++;
    }
    if(i == 10)
    {
        return 0;
    }
    return 1;
}

void _io_read_config(void)
{
    GFX_Resolution res = {640, 360, 0};
    int son = 0;

    FILE *pFile = fopen("config/config.txt", "r");

    if(pFile != 0)
    {
        fscanf(pFile, "%d", &res.width);
        fscanf(pFile, "%d", &res.height);
        fscanf(pFile, "%d", &res.multisampling);
        fscanf(pFile, "%d", &son);
        fclose(pFile);
    }

    gfx_set_resolution(res);
    sfx_set_sound_state(son);
}

void _io_write_config(void)
{
    GFX_Resolution res;
    int son;

    gfx_get_resolution(&res);
    sfx_get_sound_state(&son);

    FILE *fp = fopen("config/config.txt", "w");

    if(fp != 0)
    {
        fprintf(fp, "%d\n", res.width);
        fprintf(fp, "%d\n", res.height);
        fprintf(fp, "%d\n", res.multisampling);
        fprintf(fp, "%d\n", son);
        fclose(fp);
    }
}

void _io_read_highscores()
{
    FILE *pFile = fopen("config/highscores.txt", "r");
        if(pFile == 0)
        {
            return;
        }

        for(int i = 0; i < 10; i++)
        {
            fread(_io_scores[i].name, 3, 1, pFile);
            fscanf(pFile, "%d\n", &_io_scores[i].score);
        }
        fclose(pFile);
}

void _io_write_highscores()
{
    FILE *fp = fopen("config/highscores.txt", "w");
    for(int i = 0; i < 10; i++)
    {
        fputs(_io_scores[i].name, fp);
        fprintf(fp, " %d\n", _io_scores[i].score);
    }
    fclose(fp);
}

int io_player_get_buttons_menu(void)
{
    io_player_read_buttons();

    static int player_key_old;
    int player_key_current;
    int player_key;

    player_key_current = io_player_get_buttons(0);
    player_key = ~player_key_old & player_key_current;
    player_key_old = player_key_current;

    return player_key;
}

void io_init(void)
{
    io_highscore_reset();
    _io_read_highscores();
    _io_read_config();
    _io_init();
}
void io_exit(void)
{
    _io_write_highscores();
    _io_write_config();
    _io_exit();
}
